package com.happystreet
{
	import com.happystreet.config.GameConfig;
	import com.happystreet.engine.BottomPanel;
	import com.happystreet.engine.CoreObject;
	import com.happystreet.engine.GameEngine;
	import com.happystreet.engine.MainUI;
	import com.happystreet.engine.StreetBackground;
	import com.happystreet.engine.StreetView;
	import com.happystreet.interfaces.IApplication;
	import com.happystreet.interfaces.ILoader;
	import com.happystreet.manager.CollectionsManager;
	import com.happystreet.manager.GameManager;
	import com.happystreet.manager.ScreenManager;
	import com.happystreet.manager.SettingsManager;
	import com.happystreet.manager.SoundManager;
	import com.happystreet.manager.StreetManager;
	import com.happystreet.rpc.RpcClient;
	
	import flash.events.Event;
	
	public class Game extends GameEngine
	{
		private static var _instance:Game;
		private var _rpcClient:RpcClient;
		private var _gameManager:GameManager;
		
		public var bottomLayer:CoreObject;
		public var middleLayer:CoreObject;
		public var topLayer:CoreObject;
		public var myStreetLoader:ILoader;
		public var street:StreetManager;
		public var camera:StreetView;
		public var panel:BottomPanel;
		public var ui:MainUI;
		public function Game()
		{
			super();
			Game._instance = this;
		}
		
		public static function get instance():Game
		{
			return _instance;	
		}
		
		/**
		 *初始化
		 * 
		 */		
		protected function init():void
		{
			_rpcClient = RpcClient.getInstance();
			this.addEventListener(Event.ADDED_TO_STAGE,onAddToStageHandle);
		}
		
		
		/**
		 *添加到舞台事件处理 
		 * @param event
		 * 
		 */		
		private function onAddToStageHandle(event:Event):void
		{
			//添加场景容器到显示列表
			this.addChild(GameEngine.instance.theScene);
			bottomLayer = new CoreObject();
			middleLayer = new CoreObject();
			topLayer = new CoreObject();
			addToScene(bottomLayer);
			addToScene(middleLayer);
			addToScene(topLayer);
			//请求用户初始化数据
			_rpcClient.getInitData(initDataCallBack);
		}
		/**
		 *初始化数据回调 
		 * 
		 */		
		private function initDataCallBack():void
		{
			trace("Init game");
			SettingsManager.setLocalSharedObjectPath(GameConfig.gameBasePath);
			var soundManager:SoundManager = new SoundManager();
			var screenManager:ScreenManager = new ScreenManager();
			//初始化街道背景层
			var streetBackground:StreetBackground = new StreetBackground();
			this.bottomLayer.add(streetBackground);
			var collectionsManager:CollectionsManager = new CollectionsManager();
			this.street = new StreetManager();
			this.bottomLayer.add(street);
			this.street.buildStreet(GameConfig.userInfo);
			camera = new StreetView();
			camera.registerLayer(streetBackground);
			camera.registerLayer(this.street);
			camera.registerForDrag(this.street);
			camera.registerButtonsLayer(this.middleLayer);
			camera.setNavigationAvailable(false);
			panel = new BottomPanel();
			this.topLayer.add(panel);
			ui = new MainUI();
			this.topLayer.add(ui);
			_gameManager = GameManager.getInstance();
			myStreetLoader.unloadAssets();
			this.go();
		}
		
		private function go():void
		{
			//更改游戏模式为自己家的街道模式
			this._gameManager.changeGameMode(GlobalGameConstants.HOME_STREET_GAMEMODE);
			//激活所有的商店
			this._gameManager.dispatchEvent(new Event(GameManager.ACTIVATE_ALL_SHOPS));
		}
	}
}